package tests;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import models.HangMan1;
import models.Hangman3;
import tools.DateHeader;
import tools.SeedRNGCommandLine;

/**
 * 
 * @author J Paul Gibson<br>
 * 
 *         Random play of hangman game to illustrate functionality<br>
 *         Expected typical output:<br>
 * 
 *         <pre>
 *  The seed used for the random number generator in the test is 0.
 * You can override this value by passing an integer value as a main argument parameter, if you so wish.
 * 
 * 
 * *******************************************************************
 * Execution Date/Time 2011/11/07 17:50:57
 * *******************************************************************
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --cd---h-j---n-pqr---v-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: -bc---g-----mn----st-vw-y-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: ??d?g?
 * Letters Tried: ---d--g--jk----p-rst-v-xyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: o?ange
 * Letters Tried: abc-efg--j-l-nopq-s-u-w---
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: green
 * Letters Found: ?????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: green
 * Letters Found: ?r???
 * Letters Tried: a--d-f-h-j--m-op-r--u---y-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: ?n??g?
 * Letters Tried: -b--e-gh-j-lmn----s---wx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: o??n??
 * Letters Tried: -b-d----ijk--no----t-vwx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: r?d
 * Letters Tried: -bcd------k-m-opqrs-----yz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: green
 * Letters Found: ?????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: green
 * Letters Found: gr??n
 * Letters Tried: ab---fg----lmnopqr-t---x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ?ra???
 * Letters Tried: a-c--f---jkl---pqrs--v-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: blue
 * Letters Found: ????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: blue
 * Letters Found: ?l??
 * Letters Tried: --cd-fgh---l-nop-r----w---
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ??a???
 * Letters Tried: a-c----h--klm---q-s---wxy-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: yellow
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: yellow
 * Letters Found: yell?w
 * Letters Tried: a-cde-g---klm-----st--wxyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: red
 * Letters Found: ???
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: red
 * Letters Found: re?
 * Letters Tried: ----e--h--klm-op-rs-uv-x--
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: violet
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: violet
 * Letters Found: vio?e?
 * Letters Tried: ab-de-ghij----opq---uv---z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: orange
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: orange
 * Letters Found: ?r??g?
 * Letters Tried: --c---ghij------qrs-u--xyz
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: indigo
 * Letters Found: i??ig?
 * Letters Tried: ab--e-g-ij------qr--u-wx-z
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: violet
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 10 from 10
 * Current Word: violet
 * Letters Found: v?ole?
 * Letters Tried: --cdefg----l--opqr--uv-xy-
 * 
 * ******Starting A New Game********
 * 
 * Hangman3 -- Errors: 0 from 10
 * Current Word: indigo
 * Letters Found: ??????
 * Letters Tried: --------------------------
 * 
 * Hangman3 -- Errors: 5 from 10
 * Current Word: indigo
 * Letters Found: indigo
 * Letters Tried: a--d--g-i----no---s--v-x-z
 * 
 * ******Congratulations - you have finally won ********
 * </pre>
 * 
 */
public class HangMan1RandomTest {

	public static void main(String[] args) {

		// Random rng = SeedRNGCommandLine.getRandom(args);
		Random rng;

		/**
		 * The seed for generating random tests is, by default, set to 0; but
		 * can be over-ridden by a command line parameter
		 */
		long rngSeed = 0;

		if (args.length == 0) {
			System.out
					.println("The seed used for the random number generator in the test is "
							+ rngSeed
							+ ".\nYou can override this value by passing an integer value as a main argument parameter,"
							+ " if you so wish.\n");
		}

		else
			try {
				rngSeed = Integer.parseInt(args[0]);
				System.out
						.println("The seed  used for the random number generator in the test is "
								+ rngSeed);
			} catch (NumberFormatException exc) {
				System.out.println("First argument was not an integer");
				System.out
						.println("The seed  used for the random number generator in the test is "
								+ rngSeed);
			}

		rng = new Random(rngSeed);

		// System.out.println(DateHeader.dateString());
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new java.util.Date();
		String str = "\n********************************************************************";
		str = str + "\nExecution Date/Time " + dateFormat.format(date);
		str = str
				+ "\n********************************************************************";
		System.out.println(str);

		String[] colours = { "red", "orange", "yellow", "green", "blue",
				"indigo", "violet" };

		// Hangman3 hangman3 = new Hangman3(rng, colours);
		HangMan1 hangman1 = new HangMan1(rng, colours);

		// foundword
		boolean foundword = true;
		for (int i = 0; i < hangman1.currentWord.length(); i++)
			if (!hangman1.lettersFound[i])
				foundword = false;

		while (!foundword) {

			System.out.println("\n*******Starting A New Game********");
			// hangman3.randomWord();

			int size = hangman1.allWords.size();
			if (size == 0)
				throw new IllegalStateException(
						"Can't get a word from an empty word store");
			hangman1.currentWord = hangman1.allWords.elementAt(rng.nextInt(size));

			// System.out.println(hangman3);

			String stri = "\nHangman -- Errors: " + hangman1.errorCount
					+ " from " + hangman1.ERROR_MAX;
			stri = stri + "\nCurrent Word: " + hangman1.currentWord;

			String lettersInWord = "";
			for (int i = 0; i < hangman1.currentWord.length(); i++) {
				if (hangman1.lettersFound[i])
					lettersInWord = lettersInWord
							+ hangman1.currentWord.charAt(i);
				else
					lettersInWord = lettersInWord + '?';
			}

			stri = stri + "\nLetters Found: " + lettersInWord;

			String lettersTried = "";
			for (int i = 0; i < hangman1.ALPHABET_SIZE; i++)

				if (hangman1.lettersTried[i])
					lettersTried = lettersTried + ((char) ((int) ('a') + i));
				else
					lettersTried = lettersTried + "-";

			stri = stri + "\nLetters Tried: " + lettersTried;

			stri = stri + "\nWon game is " + hangman1.won;
			stri = stri + "\nLost game is " + hangman1.lost;
			System.out.println(stri);
			// /////////////////////////////////////////////////////

			// return foundWord() || triesLeft()<=0;
			boolean gameOver = true;
			boolean foundWord = true;
			int triesLeft = hangman1.ERROR_MAX - hangman1.errorCount;
			for (int i = 0; i < hangman1.currentWord.length(); i++)
				if (!hangman1.lettersFound[i])
					foundWord = false;

			while (!(foundWord || triesLeft <= 0)) {
				// hangman1.tryLetter((char) ('a' + rng.nextInt(26)));
				//rng = new Random(rngSeed);
				
				char letter = (char) ('a' + (rng.nextInt(26)*System.currentTimeMillis())%26);

				/***********************************/
				if (hangman1.won){
					break;
//					throw new IllegalStateException(
//							"Game is over: you already found the word");
				}
				if (hangman1.lost){
					break;
					//throw new IllegalStateException(
						//	"Game is over: no more attempts left");
				}
				if (letter < hangman1.ALPHABET_FROM
						|| letter > hangman1.ALPHABET_TO)
					break;
					//throw new IllegalArgumentException(
						//	"Letters must be in alphabet");
				boolean found = false;
				int index = letter - (int) (hangman1.ALPHABET_FROM);

				if (hangman1.lettersTried[index])
					;

				else {
					hangman1.lettersTried[index] = true;
					for (int i = 0; i < hangman1.currentWord.length(); i++)
						if (hangman1.currentWord.charAt(i) == letter) {
							hangman1.lettersFound[i] = true;
							found = true;
						}
					if (!found) {
						hangman1.errorCount++;
						if (hangman1.errorCount > hangman1.ERROR_MAX)
							hangman1.lost = true;
					}
					
					boolean ffoundWord = true;
					for (int i = 0; i < hangman1.currentWord.length(); i++)
						if (!hangman1.lettersFound[i])
							ffoundWord = false;

					hangman1.won = ffoundWord;
					hangman1.lost = (hangman1.ERROR_MAX - hangman1.errorCount) < 1;				

				}
				/***********************************/

			}
			// System.out.println(hangman3);
			stri = "\nHangman -- Errors: " + hangman1.errorCount + " from "
					+ hangman1.ERROR_MAX;
			stri = stri + "\nCurrent Word: " + hangman1.currentWord;

			lettersInWord = "";
			for (int i = 0; i < hangman1.currentWord.length(); i++) {
				if (hangman1.lettersFound[i])
					lettersInWord = lettersInWord
							+ hangman1.currentWord.charAt(i);
				else
					lettersInWord = lettersInWord + '?';
			}

			stri = stri + "\nLetters Found: " + lettersInWord;

			lettersTried = "";
			for (int i = 0; i < hangman1.ALPHABET_SIZE; i++)

				if (hangman1.lettersTried[i])
					lettersTried = lettersTried + ((char) ((int) ('a') + i));
				else
					lettersTried = lettersTried + "-";

			stri = stri + "\nLetters Tried: " + lettersTried;

			stri = stri + "\nWon game is " + hangman1.won;
			stri = stri + "\nLost game is " + hangman1.lost;
			System.out.println(stri);
			// //////////////////////////////////////

			foundword = true;
			for (int i = 0; i < hangman1.currentWord.length(); i++)
				if (!hangman1.lettersFound[i])
					foundword = false;
		}

		System.out
				.println("\n*******Congratulations - you have finally won ********");

	}
}